const {DataTypes} = require('sequelize');
const seq = require('../db/seq');
const moment = require('moment');

//创建User模型
const User = seq.define('user', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    username: {
        type: DataTypes.STRING
    },
    password: {
        type: DataTypes.STRING
    },
    name: {
        type: DataTypes.STRING
    },
    score: {
        type: DataTypes.INTEGER,
        defaultValue: 0
    },
    url: {
        type: DataTypes.STRING(1000)
    },
    createdAt: {
        type: DataTypes.DATE,
        get() {
            return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss');
        }
    }
});

//创建Match模型
const Match = seq.define('match', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    initiatorId: {
        type: DataTypes.INTEGER//发起人
    },
    recipientId: {
        type: DataTypes.INTEGER//接收人
    },
    chessData: {
        type: DataTypes.JSON//{turn:1,data:[]}棋子数据，棋局结束后需要清除，防止数据过大
    },
    winnerId: {
        type: DataTypes.INTEGER,//胜利者
    },
    state: {
        type: DataTypes.INTEGER,//0-未结束，1-已结束
    },
    createdAt: {
        type: DataTypes.DATE,
        get() {
            return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss');
        }
    }
});
seq.sync({alter: true}).then(() => {

});
module.exports = {
    User,
    Match
};
